Completed
Push — master ( ad7bcf...5907f1 )
by Sander
9s
created

angular.controller(ꞌShareCtrlꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
(function () {
2
	'use strict';
3
4
	/**
5
	 * @ngdoc function
6
	 * @name passmanApp.controller:MainCtrl
7
	 * @description
8
	 * # MainCtrl
9
	 * Controller of the passmanApp
10
	 * This file is part of passman, licensed under AGPLv3
11
	 */
12
	angular.module('passmanApp')
13
		.controller('ShareCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'ShareService', 'NotificationService', 'SharingACL', 'EncryptService', 'FileService',
14
			function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, ShareService, NotificationService, SharingACL, EncryptService, FileService) {
0 ignored issues
show
Unused Code introduced by
The parameter FileService is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
15
				$scope.active_vault = VaultService.getActiveVault();
16
17
				$scope.tabs = [{
18
					title: 'Share with users and groups',
19
					url: 'views/partials/forms/share_credential/basics.html'
20
				}, {
21
					title: 'Share link',
22
					url: 'views/partials/forms/share_credential/link_sharing.html',
23
					color: 'green'
24
				}];
25
				$scope.currentTab = {
26
					title: 'General',
27
					url: 'views/partials/forms/share_credential/basics.html'
28
				};
29
30
				$scope.onClickTab = function (tab) {
31
					$scope.currentTab = tab;
32
				};
33
34
				$scope.isActiveTab = function (tab) {
35
					return tab.url === $scope.currentTab.url;
36
				};
37
38
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
39
					if (!$scope.active_vault) {
40
						$location.path('/');
41
					}
42
				} else {
43
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
44
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
45
						_vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
46
						VaultService.setActiveVault(_vault);
47
						$scope.active_vault = _vault;
48
49
					}
50
				}
51
				var storedCredential = SettingsService.getSetting('share_credential');
52
53
				if (!storedCredential) {
54
					$location.path('/vault/' + $routeParams.vault_id);
55
				} else {
56
					$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
57
				}
58
59
				if ($scope.active_vault) {
60
					$scope.$parent.selectedVault = true;
61
				}
62
				$scope.cancel = function () {
63
					SettingsService.setSetting('share_credential', null);
64
					$location.path('/vault/' + $routeParams.vault_id);
65
				};
66
67
68
				$scope.default_permissions = new SharingACL(0);
69
				$scope.default_permissions.addPermission(
70
					$scope.default_permissions.permissions.READ |
71
					$scope.default_permissions.permissions.WRITE |
72
					$scope.default_permissions.permissions.FILES
73
				);
74
75
				var link_acl = angular.copy($scope.default_permissions);
76
				link_acl.removePermission($scope.default_permissions.permissions.WRITE);
77
78
				$scope.share_settings = {
79
					linkSharing: {
80
						enabled: false,
81
						settings: {
82
							expire_time: new Date("2999-12-31T22:59:59"),
83
							expire_views: 5,
84
							acl: link_acl
85
						}
86
					},
87
					credentialSharedWithUserAndGroup: [],
88
					cypher_progress: {
89
						done: 0,
90
						total: 0
91
					},
92
					upload_progress: {
93
						done: 0,
94
						total: 0
95
					}
96
				};
97
98
				var getAcl = function () {
99
					ShareService.getSharedCredentialACL($scope.storedCredential).then(function (aclList) {
100
						var _list = [];
101
						var enc_key = ($scope.storedCredential.shared_key) ? EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)) : false;
102
						for (var i = 0; i < aclList.length; i++) {
103
							var acl = aclList[i];
104
							if (acl.user_id === null) {
105
								$scope.share_settings.linkSharing = {
106
									enabled: true,
107
									settings: {
108
										expire_time: new Date(acl.expire * 1000),
109
										expire_views: acl.expire_views,
110
										acl: new SharingACL(acl.permissions)
111
									}
112
								};
113
								if (enc_key) {
114
									var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key);
115
									$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
116
								}
117
							} else {
118
								var obj = {
119
									userId: acl.user_id,
120
									displayName: acl.user_id,
121
									type: 'user',
122
									acl: new SharingACL(acl.permissions),
123
									acl_id: acl.acl_id,
124
									pending: acl.pending,
125
									credential_guid: acl.item_guid,
126
									created: acl.created
127
								};
128
129
								_list.push(obj);
130
							}
131
132
						}
133
						$scope.share_settings.credentialSharedWithUserAndGroup = _list;
134
					});
135
				};
136
				getAcl();
137
				var acl = new SharingACL(0);
138
139
140
				$scope.$watch('share_settings.upload_progress.done', function () {
141
										if ($scope.share_settings.upload_progress.done === $scope.share_settings.upload_progress.total && $scope.share_settings.upload_progress.total > 0) {
142
						getAcl();
143
					}
144
				});
145
146
				$scope.inputSharedWith = [];
147
				$scope.selectedAccessLevel = '1';
148
149
				$scope.searchUsers = function ($query) {
150
					return ShareService.search($query);
151
				};
152
153
				$scope.hasPermission = function (acl, permission) {
154
					return acl.hasPermission(permission);
155
				};
156
157
				$scope.setPermission = function (acl, permission) {
158
					acl.togglePermission(permission);
159
				};
160
				$scope.shareWith = function (shareWith, selectedAccessLevel) {
0 ignored issues
show
Unused Code introduced by
The parameter selectedAccessLevel is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
161
					$scope.inputSharedWith = [];
162
					if (shareWith.length > 0) {
163
						for (var i = 0; i < shareWith.length; i++) {
164
							var obj = {
165
								userId: shareWith[i].uid,
166
								displayName: shareWith[i].text,
167
								type: shareWith[i].type,
168
								acl: angular.copy($scope.default_permissions),
169
								pending: true,
170
								credential_guid: $scope.storedCredential.guid
171
							};
172
							var found = false;
173
							for (var z = 0; z < $scope.share_settings.credentialSharedWithUserAndGroup.length; z++) {
174
								if ($scope.share_settings.credentialSharedWithUserAndGroup[z].userId === shareWith[z].uid) {
175
									found = true;
176
								}
177
							}
178
							if (found === false) {
179
								$scope.share_settings.credentialSharedWithUserAndGroup.push(obj);
180
							}
181
						}
182
					}
183
				};
184
185
				$scope.unshareUser = function (user) {
186
					ShareService.unshareCredentialFromUser($scope.storedCredential, user.userId).then(function (result) {
187
						if (result.result === true) {
188
							var idx = $scope.share_settings.credentialSharedWithUserAndGroup.indexOf(user);
189
							$scope.share_settings.credentialSharedWithUserAndGroup.splice(idx, 1);
190
						}
191
					});
192
				};
193
194
				$scope.unshareCredential = function (credential) {
195
					ShareService.unshareCredential(credential);
196
					var _credential = angular.copy(credential);
197
					var old_key = EncryptService.decryptString(angular.copy(_credential.shared_key));
198
					var new_key = VaultService.getActiveVault().vaultKey;
199
					_credential.shared_key = null;
200
					_credential.unshare_action = true;
201
					_credential.skip_revision = true;
202
203
					_credential = CredentialService.encryptCredential(_credential, old_key);
204
					CredentialService.updateCredential(_credential, true).then(function () {
205
						NotificationService.showNotification('Credential unshared', 4000);
206
						CredentialService.reencryptCredential(_credential.guid, old_key, new_key).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
207
							getAcl();
208
						});
209
					});
210
				};
211
212
				/**
213
				 * Apply a share to a new user
214
				 * @param user A user object to who we should share the data
215
				 * @param enc_key The shared key we are going to ecnrypt with his public rsa key
216
				 */
217
				$scope.applyShareToUser = function (user, enc_key) {
218
					ShareService.getVaultsByUser(user.userId).then(function (data) {
219
						$scope.share_settings.cypher_progress.total += data.length;
220
221
						user.vaults = data;
222
						var start = new Date().getTime() / 1000;
223
						ShareService.cypherRSAStringWithPublicKeyBulkAsync(user.vaults, enc_key)
224
							.progress(function () {
225
								$scope.share_settings.cypher_progress.done++;
226
								$scope.share_settings.cypher_progress.percent = $scope.share_settings.cypher_progress.done / $scope.share_settings.cypher_progress.total * 100;
227
								$scope.$digest();
228
							})
229
							.then(function (result) {
230
																$scope.share_settings.cypher_progress.times.push({
231
									time: ((new Date().getTime() / 1000) - start),
232
									user: data[0].user_id
233
								});
234
								user.vaults = result;
235
								if (!user.hasOwnProperty('acl_id')) {
236
									$scope.uploadChanges(user);
237
								}
238
								$scope.$digest();
239
							});
240
					});
241
				};
242
243
244
				$scope.sharing_complete = true;
245
				$scope.applyShare = function () {
246
					$scope.sharing_complete = false;
247
					$scope.share_settings.cypher_progress.percent = 0;
248
					$scope.share_settings.cypher_progress.done = 0;
249
					$scope.share_settings.cypher_progress.total = 0;
250
					$scope.share_settings.cypher_progress.times = [];
251
					$scope.share_settings.cypher_progress.times_total = [];
252
					$scope.share_settings.upload_progress.done = 0;
253
					$scope.share_settings.upload_progress.total = 0;
254
					//Credential is already shared
255
					if ($scope.storedCredential.shared_key && $scope.storedCredential.shared_key !== '' && $scope.storedCredential.shared_key !== null) {
256
												var enc_key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
257
						if ($scope.share_settings.linkSharing.enabled) {
258
							var expire_time = new Date(angular.copy($scope.share_settings.linkSharing.settings.expire_time)).getTime() / 1000;
259
							var shareObj = {
260
								item_id: $scope.storedCredential.credential_id,
261
								item_guid: $scope.storedCredential.guid,
262
								permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
263
								expire_timestamp: expire_time,
264
								expire_views: $scope.share_settings.linkSharing.settings.expire_views
265
							};
266
							ShareService.createPublicSharedCredential(shareObj).then(function () {
267
								var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key);
268
								$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
269
							});
270
						}
271
272
						var list = $scope.share_settings.credentialSharedWithUserAndGroup;
273
274
						for (var i = 0; i < list.length; i++) {
275
							var iterator = i;
276
							var target_user = list[i];
277
							if (target_user.hasOwnProperty('created')) {
278
								var acl = {
279
									user_id: target_user.userId,
280
									permission: target_user.acl.getAccessLevel()
281
								};
282
								ShareService.updateCredentialAcl($scope.storedCredential, acl);
283
							} else {
284
								$scope.applyShareToUser(list[iterator], enc_key);
285
							}
286
						}
287
288
					} else {
289
290
						ShareService.generateSharedKey(20).then(function (key) {
291
292
							var encryptedSharedCredential = angular.copy($scope.storedCredential);
293
							var old_key = VaultService.getActiveVault().vaultKey;
294
295
							CredentialService.reencryptCredential(encryptedSharedCredential.guid, old_key, key).progress(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
296
															}).then(function (data) {
297
								var _credential = data.cryptogram;
298
								_credential.set_share_key = true;
299
								_credential.skip_revision = true;
300
								_credential.shared_key = EncryptService.encryptString(key);
301
								CredentialService.updateCredential(_credential, true).then(function () {
302
									NotificationService.showNotification('Credential shared', 4000);
303
									$scope.sharing_complete = true;
304
								});
305
							});
306
307
							var list = $scope.share_settings.credentialSharedWithUserAndGroup;
308
							for (var i = 0; i < list.length; i++) {
309
								if (list[i].type === "user") {
310
									$scope.applyShareToUser(list[i], key);
311
								}
312
							}
313
314
							if ($scope.share_settings.linkSharing.enabled) {
315
								var expire_time = new Date(angular.copy($scope.share_settings.linkSharing.settings.expire_time)).getTime() / 1000;
316
								var shareObj = {
317
									item_id: $scope.storedCredential.credential_id,
318
									item_guid: $scope.storedCredential.guid,
319
									permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
320
									expire_timestamp: expire_time,
321
									expire_views: $scope.share_settings.linkSharing.settings.expire_views
322
								};
323
								ShareService.createPublicSharedCredential(shareObj).then(function () {
324
									var hash = window.btoa($scope.storedCredential.guid + '<::>' + key);
325
									$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
326
327
								});
328
							}
329
330
						});
331
					}
332
				};
333
334
				$scope.uploadChanges = function (user) {
335
					$scope.share_settings.upload_progress.total++;
336
337
					user.accessLevel = angular.copy(user.acl.getAccessLevel());
338
					ShareService.shareWithUser(storedCredential, user)
339
						.then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
340
							$scope.share_settings.upload_progress.done++;
341
							$scope.share_settings.upload_progress.percent = $scope.share_settings.upload_progress.done / $scope.share_settings.upload_progress.total * 100;
342
						});
343
				};
344
345
				$scope.calculate_total_time = function () {
346
					$scope.share_settings.cypher_progress.times = $scope.share_settings.cypher_progress.times || [];
347
					var total = 0;
348
					for (var i = 0; i < $scope.share_settings.cypher_progress.times.length; i++) {
349
						total += $scope.share_settings.cypher_progress.times[i].time;
350
					}
351
					return total;
352
				};
353
			}]);
354
}());